import { ref } from "vue"
import { etfPriceReq, bondPriceReq, stockPriceReq } from '@/api/stock'
import type { InnerData } from '@/api/stock'
import { ElMessage } from "element-plus"

export const useInnerStock = () => {
  const stockInfo = ref<InnerData>()
  const reqList = [etfPriceReq, bondPriceReq, stockPriceReq]
  let nowIndex = 0
  const getPriceInfo = async (code: string) => {
    if (!code || code.length !== 6) {
      ElMessage.error('请输入正确的代码!')
      return Promise.reject('请输入正确的代码')
    }
    await reqList[nowIndex]([code]).then(res => {
      if (res) {
        nowIndex = 0
        stockInfo.value = res
      } else {
        if (nowIndex < reqList.length - 1) {
          nowIndex++
          getPriceInfo(code)
        } else {
          ElMessage.error('仅支持场内品种!')
        }
      }
    })

  }
  return {
    stockInfo,
    getPriceInfo
  }
}